package com.ti.shangou.controller.pages.back.approval;

import com.ti.shangou.controller.BaseController;
import com.ti.shangou.dao.MerchantDao;
import com.ti.shangou.pojo.dto.PageDTO;
import com.ti.shangou.pojo.entity.ApprovalLog;
import com.ti.shangou.pojo.entity.Merchant;
import com.ti.shangou.pojo.query.ApprovalLogQuery;
import com.ti.shangou.pojo.vo.UserVO;
import com.ti.shangou.service.impl.ApprovalLogServiceImpl;
import com.ti.shangou.service.impl.MerchantServiceImpl;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.Date;

/**
 * 作者：田宝山
 * 日期：2020/6/5
 */
@Controller
@RequestMapping("/pages/back/approval")
public class ApprovalController extends BaseController {
    @Resource
    ApprovalLogServiceImpl approvalLogService;
    @Resource
    MerchantDao merchantDao;

    // 分页商户语句
    @ResponseBody
    @RequestMapping("getMerchantLogsById/{merchantId}")
    PageDTO getMerchantLogsById(ApprovalLogQuery query) {
        return approvalLogService.getMerchantLogsByQuery(query);
    }

    @ResponseBody
    @RequestMapping("doApproval/{merchantId}")
    public Object doApproval(@PathVariable("merchantId") Long merchantId, ApprovalLog approvalLog){
        UserVO user = (UserVO) getSession().getAttribute("user");
        approvalLog.setApprovalUserId(user.getUserId());
        approvalLog.setApprovalUserName(user.getRealName());
        approvalLog.setCreateTime(new Date());
        approvalLog.setMerchantId(merchantId);

        Merchant merchant = new Merchant();
        merchant.setMerchantId(merchantId);
        merchant.setApprovalStatus(approvalLog.getRes());
        merchant.setUpdateTime(new Date());
        merchantDao.updateByPrimaryKeySelective(merchant);
        return approvalLogService.addApproval(approvalLog);
    }
}
